import grails.test.*


class LoginControllerTests extends ControllerUnitTestCase {

  def HmockDomain

  protected void setUp() {
    super.setUp()
  }

  protected void tearDown() {
    super.tearDown()
  }

  void testLogin() {
    // for cobertura completeness
    controller.login()
  }

  void testLogout() {

    mockSession.user = new UserAccount()
    mockSession.user.id = 99
    mockDomain(UserAccount, [mockSession.user])

    assertNotNull(mockSession.user)
    controller.logout()
    assertNull(mockSession.user)

  }

  void testHandleLogin() {

    def user = new UserAccount(firstName: "Dale", lastName: "Manthei", email: "d@g.com", password: "pass")
    mockDomain(UserAccount, [user])

    // Wrong password
    controller.params.email = "d@g.com"
    controller.params.password = "wrong"
    controller.handleLogin()
    assertNull(mockSession.user)
    assertEquals('flash message',
            "User not found for email: ${controller.params.email}",
            mockFlash.message)

    // Wrong email
    controller.params.email = "x@y.com"
    controller.params.password = "pass"
    controller.handleLogin()
    assertNull(mockSession.user)
    assertEquals('flash message',
            "User not found for email: ${controller.params.email}",
            mockFlash.message)

    // Missing email
    controller.params.email = ""
    controller.params.password = "pass"
    controller.handleLogin()
    assertNull(mockSession.user)
    assertEquals('flash message',
            "User not found for email: ${controller.params.email}",
            mockFlash.message)

    // Missing password
    controller.params.email = "d@g.com"
    controller.params.password = ""
    controller.handleLogin()
    assertNull(mockSession.user)
    assertEquals('flash message',
            "User not found for email: ${controller.params.email}",
            mockFlash.message)

    // Correct signin
    controller.params.email = "d@g.com"
    controller.params.password = "pass"
    controller.handleLogin()
    assertEquals(user, mockSession.user)
    assertEquals(redirectArgs.uri, "/")

  }

}
